import http from '@/tool/http'
import axios from 'axios'
import url from '@/tool/api'
const state = {
	user:{
		name:sessionStorage.getItem('name') || undefined,
		permissions:sessionStorage.getItem('permissions') || undefined,
		// token:sessionStorage.getItem('token') || undefined
		per_id:sessionStorage.getItem('per_id') || undefined,
		type:sessionStorage.getItem('type') || undefined,
	},
	yggl:{
		tableData:[],
		jobSelect:[],
		roleSelect:[],
		storeSelect:[],
		count:0,
		role:[],
	}
}
const mutations = {
	addUser(state,obj){
		let type = 2
		if(obj.per_id===7){
			type = 1
		}
		sessionStorage.setItem('type',type)
		state.user.name = obj.name
		state.user.permissions = obj.permissions
		state.user.per_id = obj.per_id
		state.user.type = type
	},
	muOutlogin(state){
		sessionStorage.clear()
		state.user.name = undefined
		state.user.permissions = undefined
		state.user.per_id = undefined
		state.user.id = undefined
		state.user.type = undefined
	},
	setSelect(state,obj){
		state.yggl.jobSelect = obj.jobSelect
		state.yggl.roleSelect = obj.roleSelect
		state.yggl.storeSelect = obj.storeSelect
		state.yggl.role = obj.role
	},
	setYgTableData(state,obj){
		state.yggl.tableData = obj
	},
	setYgCount(state,num){
		state.yggl.count = num
	},
	delyg(state){
		state.yggl.count -= 1
	}
}
const actions = {
	actLogin({commit},obj){
		// console.log(commit)
		// console.log(obj)
		return http({
			url:url.login,
			method:'post',
			data:obj
		}).then(res=>{
			if(res.data.code===200){
				//权限 1 员工 2 会计 3 普通管理员 4超级管理员
				switch (res.data.permissions){
					case '服务员':
					res.data.permissions = 1
					break;
					case '会计' :
					res.data.permissions = 2
					break;
					case '普通管理员' :
					res.data.permissions = 3
					break;
					case '超级管理员' :
					res.data.permissions =4
					break;
				}
				sessionStorage.setItem('name',res.data.name)
				sessionStorage.setItem('permissions',res.data.permissions)
				sessionStorage.setItem('number',obj.number)
				sessionStorage.setItem('id',res.data.id)
				sessionStorage.setItem('per_id',res.data.per_id)
				commit('addUser',res.data)
			}else{
				sessionStorage.setItem('name','')
				sessionStorage.setItem('permissions','')
			}
			return res.data
		})
	},
	actOutlogin({commit}){
		console.log('actons')
		commit('muOutlogin')
	},
	actGetygData({commit},num){
		
		// console.log(obj)
		if(num === undefined){
			num = 0
		}
		// console.log(num)
		return new Promise((resolve,reject)=>{
			http({
				url:url.getygCount,
				method:'post'
			}).then(res=>{
				commit('setYgCount',res.data.count)
			})
			resolve(1)
		}).then(res=>{
			http({
				url:url.getygData,
				method:'post',
				data:{
					num1:num,
					num2:5,
				}
			}).then(res=>{
				console.log(res)
				if(res.data.code==='200'){
					// console.log(res.data.data)
					commit('setYgTableData',res.data.data)
				}
			})
		})
		//发起获取员工信息的请求
	},
	actGetSelectData({commit}){
		//发起请求 获取jobSelect roleSelect storeSelect的值
		let obj = {
			jobSelect:[],
			roleSelect:[],
			storeSelect:[],
			role:[]
		}
		http({
			url:url.getJobName,
			method:'post'
		}).then(res=>{
			// console.log(res)
			if(res.data.code==='200'){
				res.data.data.filter(item=>{
					// obj.jobSelect.push(item.name)
					obj.jobSelect.push(item)
				})
			}
			
			// obj.jobSelect
		})
		http({
			url:url.getRoleName,
			method:'post'
		}).then(res=>{
			// console.log(res)
			if(res.data.code==='200'){
				res.data.data.filter(item=>{
					if(item.name!='超级管理员'){
						obj.roleSelect.push(item.name)
						obj.role.push(item)
					}
				})
			}
		})
		http({
			url:url.getStoreName,
			method:'post'
		}).then(res=>{
			// console.log(res)
			if(res.data.code==='200'){
				res.data.data.filter(item=>{
					obj.storeSelect.push(item.name)
				})
			}
		})
		// console.log(obj)
		commit('setSelect',obj)
	},

	edityg({commit},obj){
		// console.log(obj)
		return http({
			url:url.updateyg,
			method:'post',
			data:obj
		}).then(res=>{
			// console.log(res)
			return res.data.code
		})
	},
	delyg({commit},obj){
		return http({
			url:url.delyg,
			method:'post',
			data:{number:obj.number}
		}).then(res=>{
			// console.log(res)
			// commit('delyg')
			return res.data.code
		})
	},
	addyg({commit},obj){
		// console.log(obj)
		return http({
			url:url.addyg,
			method:'post',
			data:obj
		}).then(res=>{
			// console.log(res)
			return res.data.code
		})
	},
	seachYg({commit},obj){
		return http({
			url:url.getygsData,
			method:'post',
			data:obj
		}).then(res=>{
			// console.log(res)
			if(res.data.code === '200'){
				commit('setYgTableData',res.data.data)
				let num = res.data.data.length
				commit('setYgCount',num)
			}
			return res.data.code
		})
	}
}
const getters = {
	jobSelect(state){
		return state.yggl.jobSelect
	}, 
	roleSelect(state){
		return state.yggl.roleSelect
	}, 
	storeSelect(state){
		return state.yggl.storeSelect
	},
	role(state){
		return state.yggl.role
	}
}

export default {
	state,mutations,actions,getters
}